package com.sri.lucene.index;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.sri.business.search.SearchManager;
import com.sri.model.home.Home;

/**
 * This class is used to create index for DB Data
 * 
 */
public class IndexManager {

	// the directory that is used to store lucene index
	private static final String indexDir = "E:\\indexDir";

	private static IndexWriter indexWriter;

	public static void indexLocations(Home bean) throws IOException {
		//index for bean - adding document for the bean
		IndexWriter writer = (IndexWriter) getIndexWriter(false);
		Document doc = new Document();
		doc.add(new Field("id", String.valueOf(bean.getId()), Field.Store.YES,
				Field.Index.NO));
		doc.add(new Field("name", bean.getName(), Field.Store.YES,
				Field.Index.TOKENIZED));
		doc.add(new Field("city", bean.getCityName(), Field.Store.YES,
				Field.Index.UN_TOKENIZED));
		doc.add(new Field("desc", bean.getMessage(), Field.Store.YES,
				Field.Index.TOKENIZED));
		doc.add(new Field("rent", String.valueOf(bean.getRent()),
				Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("zip", bean.getZipCode(), Field.Store.YES,
				Field.Index.TOKENIZED));
		doc.add(new Field("stat", bean.getStateName(), Field.Store.YES,
				Field.Index.TOKENIZED));
		doc.add(new Field("pets", String.valueOf(bean.getPetsAllowed()),
				Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("mems", String.valueOf(bean.getNoOfMembersAllowed()),
				Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("cost", String.valueOf(bean.getMaintenanceCost()),
				Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("nego", String.valueOf(bean.getRentNegotiable()),
				Field.Store.YES, Field.Index.TOKENIZED));

		doc.add(new Field("type", String.valueOf(bean.getTypeId()),
				Field.Store.YES, Field.Index.TOKENIZED));

		String fullSearchableText = bean.getName() + " " + bean.getStateName()
				+ " " + bean.getCityName() + " " + bean.getRent() + " "
				+ bean.getZipCode();

		doc.add(new Field("content", fullSearchableText, Field.Store.NO,
				Field.Index.TOKENIZED));
		writer.addDocument(doc);
	}

	public static void rebuildIndexes() throws IOException {
		getIndexWriter(true); // Erase existing index
		List<Home> allHomes = null; // Index all home entries
		try {
			allHomes = SearchManager.getALLHomes();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (Home home : allHomes) {
			indexLocations(home);
		}
		// Don't forget to close the index writer when done
		closeIndexWriter();
	}

	private static void closeIndexWriter() throws IOException {
		indexWriter.optimize();
		indexWriter.close();

	}

	/**
	 * judge if the index is already exist
	 */
	public static boolean ifIndexExist() {
		File directory = new File(indexDir);
		if (0 < directory.listFiles().length) {
			return true;
		} else {
			return false;
		}
	}

	public static IndexWriter getIndexWriter(boolean value) throws IOException {
		if (value) {
			if (true == ifIndexExist()) {
				System.out.println("Index already Exist");
				return null;
			}
			Directory fsDirectory = FSDirectory.getDirectory(indexDir, true);
			Analyzer analyzer = new StandardAnalyzer();
			indexWriter = new IndexWriter(fsDirectory, analyzer, true);
			return indexWriter;
		} else
			return indexWriter;

	}

	public static String getIndexDir() {
		return indexDir;
	}

}
